package com.ssm.controller;

import com.ssm.service.MyUserService;
import com.ssm.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.AccessDeniedException;

/**
 * Created by wchen on 2017/7/4.
 */
@Controller
public class LoginLogoutController extends BaseController {

    @Autowired
    private MyUserService myUserService;

    @RequestMapping(method= RequestMethod.GET,value="/login.do")
    public void home() {
    }
    //@RolesAllowed("ROLE_USER")
    @RequestMapping(value = { "/header" }, method = RequestMethod.POST)
    @ResponseBody
    public ResultUtils heaerPage(String username) {
        username="admin";
        String username1 = myUserService.getUser(username);

        return ResultUtils.build(200, username1, "dsds");
    }

    @RequestMapping(value = { "/" }, method = RequestMethod.POST)
    @ResponseBody
    public ResultUtils homePage(ModelMap model) {
        String user = "wc";
        return ResultUtils.ok("abc");
    }

    @RequestMapping(value = { "/index" }, method = RequestMethod.GET)
    public ResultUtils homePage1(ModelMap model) {
        String user = "wc1";
        return ResultUtils.ok("token");
    }

    @RequestMapping(value = { "/getUserDetail" }, method = RequestMethod.POST)
    @ResponseBody
    public ResultUtils userDetail(String username) {
        username="admin";
        String username1 = myUserService.getUser(username);
        return ResultUtils.build(200,"username","dsds");
    }
    /**
     * 添加对 AccessDeniedException 处理的控制器
     * 处理security.xml中
     * <security:access-denied-handler error-page="/accessDenied.do"/>配置的
     * @param model
     * @param request
     */
    @RequestMapping(method = RequestMethod.GET,value = "/accessDenied.do")
    public void accessDenied(ModelMap model,HttpServletRequest request){
/*        AccessDeniedException ex = (AccessDeniedException) request.getAttribute(AccessDeniedHandlerImpl.class.);
        StringWriter sw = new StringWriter();
        model.addAttribute("errorDetails", ex.getMessage());
        ex.printStackTrace(new PrintWriter(sw));
        model.addAttribute("errorTrace", sw.toString());*/

        Object obj = request.getAttribute("SPRING_SECURITY_403_EXCEPTION");
        if(null != obj){
            AccessDeniedException adx = (AccessDeniedException)obj;//此处未成功解决，会报错
            model.addAttribute("errorDetails",adx.getMessage());
        }

    }

    @RequestMapping(value="/logout", method = RequestMethod.POST)
    public String logout (HttpServletRequest request, HttpServletResponse response) {
        //String username = request.getHeader()
        return "login";
    }

    private String getPrincipal(){
        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((UserDetails)principal).getUsername();
        } else {
            userName = principal.toString();
        }
        return userName;
    }
}
